<?php 

// Namespace

namespace Loop;


/**
 * CFeatureManager allows to connect several additional features to objects
 */

class CFeatureManager
{
	// Properties
	
	private $features = null;
	

	/**
	 * Constructs a feature manager object
	 */

	public function __construct()
	{
		$this->features = array();
	}


	/**
	 * Returns a needed feature
	 *
	 * @param 	string 	$extensionId 	The extension of the feature
	 * @param 	string 	$featureName 	The name of the feature
	 *
	 * @return 	object 	The needed feature
	 */

	public function getFeature($extensionId, $featureName)
	{
		// Has the feature already been created?

		if (isset($this->features[$extensionId][$featureName]) == false)
		{
			// Check whether it exists

			$className = 'C' . ucfirst($featureName) . 'Feature';
			$pathToFeature = PATH_EXTENSION . $extensionId . '/Class/' . $className . '.php';

			if (file_exists($pathToFeature) == false)
			{
				e
				(
					EXCEPTION_FEATURE_NOT_FOUND,
					array
					(
						'className' => $className,
						'extension' =>$extensionId,
						'featureName' => $featureName,
						'pathToFeature' => $pathToFeature
					)
				);
			}


			// Create the feature

			require_once($pathToFeature);


			// Is there a namespace ?

			$classObject = $className;
			
			if (class_exists($className) === false)
			{
				$classObject = str_replace( '-' , '\\', $extensionId) . '\\' . $className;
			}

			$this->features[$extensionId][$featureName] = new $classObject();
		}


		return $this->features[$extensionId][$featureName];
	}


	/**
	 * Initializes a feature for the given object
	 *
	 * @param 	string 	$extensionId 	The extension of the feature
	 * @param 	string 	$featureName 	The name of the feature
	 * @param 	object 	$object 		The given object passed by reference
	 */

	public function initializeFeatureOnObject($extensionId, $featureName, &$object)
	{
		try
		{
			$feature = $this->getFeature($extensionId, $featureName);
			$feature->initializeObject($object);
		}
		catch (\Exception $e)
		{
		}
	}
}

?>
